//
//  CouponViewController.m
//  Findif
//
//  Created by Mac on 2018/11/12.
//  Copyright © 2018年 Cicel Chen. All rights reserved.
//

#import "CouponViewController.h"
#import "SettingCouponViewController.h"
#import "MyDiscountTableViewCell.h"
#import "CouponModel.h"

@interface CouponViewController ()<UITableViewDelegate,UITableViewDataSource,MyDiscountTableViewCellDelegate>

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, weak)UIView * backView;
@property (nonatomic, weak)UIImageView * backImageview;
@property (nonatomic, weak)UILabel * exploreLabel;
@property (nonatomic, strong)NSArray<CouponModel *> *couponArray;
@property (nonatomic, strong)NSIndexPath * indexPath;

@end

@implementation CouponViewController

- (void)viewWillAppear:(BOOL)animated{
    
   [self loadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = ZRWhiteColor;
    
    [self setupNav];
    [self setupUI];
}

- (void)loadData{
    
    NSString * shop_id = DEF_GET_OBJECT(@"shopid");
    
    [[ZRNetworkTool sharedTool] getMerchantsCouponsResultWithShop_id:shop_id WithUser_id:@"no" WithBlock:^(BOOL success, id responseData) {
        
//        NSLog(@"res===%@",responseData);
        if (success) {
            
            self.backView.hidden = YES;
            self.tableView.hidden = NO;
            self.couponArray = [NSArray yy_modelArrayWithClass:[CouponModel class] json:responseData[@"data"]];
        }else{
            
            self.backView.hidden = NO;
            self.tableView.hidden = YES;
        }
    }];
}


- (void)setCouponArray:(NSArray<CouponModel *> *)couponArray{
    
    _couponArray = couponArray;
    
    [self.tableView reloadData];
}

- (void)setupNav{
    
    self.title = @"优惠券";
    
//    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back(1)"] style:UIBarButtonItemStylePlain target:nil action:nil];
//    
//    self.navigationItem.backBarButtonItem = backItem;
    
    
    UIButton * rightButton = [[UIButton alloc] init];
    
    rightButton.tag = 2;
    [rightButton setImage:[UIImage imageNamed:@"add"] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(didNavButton:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * rightBar = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    
    self.navigationItem.rightBarButtonItem = rightBar;
}


- (void)setupUI{
    
    UIView * backView = [[UIView alloc] initWithFrame:self.view.bounds];
    
    backView.backgroundColor = ZRWhiteColor;
    
    self.backView = backView;
    
    [self.view addSubview:self.backView];
    
    UIImageView * backImageView = [UIImageView new];
    
    backImageView.image = [UIImage imageNamed:@"无优惠券"];
    
    self.backImageview = backImageView;
    
    [self.backView addSubview:self.backImageview];
    
    
    UILabel * exploreLabel = [UILabel new];
    
    exploreLabel.textColor = [UIColor lightGrayColor];
    exploreLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    exploreLabel.text = @"亲，你还没有设置优惠券哦！\n赶快设置优惠券吧！";
    exploreLabel.textAlignment = NSTextAlignmentCenter;
    exploreLabel.numberOfLines = 2;
    
    self.exploreLabel = exploreLabel;
    
    [self.backView addSubview:self.exploreLabel];
    
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [UIColor clearColor];
    tableView.rowHeight = 115 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView = tableView;
    
    [self.tableView registerClass:[MyDiscountTableViewCell class] forCellReuseIdentifier:@"NewCouponTableViewCell"];
    
    [self.view addSubview:self.tableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.couponArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MyDiscountTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewCouponTableViewCell" forIndexPath:indexPath];
    
//    cell.title = self.titleArray[indexPath.row];
    cell.couponModel = self.couponArray[indexPath.row];
    cell.delegate = self;
    //添加长按手势
    UILongPressGestureRecognizer * longPressGesture =[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(cellLongPress:)];
    
    longPressGesture.minimumPressDuration=1.0f;//设置长按 时间
    [cell addGestureRecognizer:longPressGesture];
    
    return cell;
}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//
//    HiddenBar;
//
//    CouponModel * model = self.couponArray[indexPath.row];
//
//    SettingCouponViewController * addVC = [SettingCouponViewController new];
//
//    addVC.titleType = @"edit";
//    addVC.model = model;
//
//    [self.navigationController pushViewController:addVC animated:YES];
//}


- (void)viewDidLayoutSubviews{
    
    [self.backImageview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view).offset(135 * SCREENH6);
        make.width.mas_equalTo(177 * SCREENH6);
        make.height.mas_equalTo(160 * SCREENH6);
    }];
    
    [self.exploreLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.backImageview.mas_bottom).offset(20 * SCREENH6);
    }];
}


- (void)cellLongPress:(UILongPressGestureRecognizer *)longRecognizer{
    
    if (longRecognizer.state == UIGestureRecognizerStateBegan) {//手势开始
        
        CGPoint point = [longRecognizer locationInView:_tableView];
        self.indexPath = [self.tableView indexPathForRowAtPoint:point]; // 可以获取我们在哪个cell上长按
        
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                       message:@"是否确认删除该优惠券？"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault
                                                              handler:^(UIAlertAction * action) {
                                                                  
                                                                  [self deleteCell];
                                                                  
                                                              }];
        UIAlertAction* cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
                                                             handler:^(UIAlertAction * action) {
                                                                 
                                                             }];
        
        [alert addAction:defaultAction];
        [alert addAction:cancleAction];
        [self presentViewController:alert animated:YES completion:nil];
    }
    
//    if (longRecognizer.state == UIGestureRecognizerStateEnded)//手势结束
//    {
//
//
//    }
}

- (void)didNavButton:(UIButton *)button{
    
    HiddenBar;
    
    SettingCouponViewController * addVC = [SettingCouponViewController new];
    
    [self.navigationController pushViewController:addVC animated:YES];
}


- (void)deleteCell{

    CouponModel * model = self.couponArray[self.indexPath.row];
    
    NSString * merchants_id = [NSString stringWithFormat:@"%@",model.merchants_id];
    NSString * couponID = [NSString stringWithFormat:@"%@",model.id];
    
    [[ZRNetworkTool sharedTool] deleteMerchantsCouponsResultWithShop_id:merchants_id WithGoods_id:couponID WithBlock:^(BOOL success, id responseData) {
        
        if (success) {
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:@"恭喜你删除成功！"
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* cancleAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel
                                                                 handler:^(UIAlertAction * action) {
                                                                     
                                                                     [self loadData];
                                                                 }];
            
            [alert addAction:cancleAction];
            [self presentViewController:alert animated:YES completion:nil];
        }else{
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:responseData[@"message"]
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* cancleAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel
                                                                 handler:^(UIAlertAction * action) {
                                                                     
                                                                     [self.tableView reloadData];
                                                                 }];
            
            [alert addAction:cancleAction];
            [self presentViewController:alert animated:YES completion:nil];
        }
       
    }];
}


- (void)clickUseButtonWithCell:(MyDiscountTableViewCell *)cell{
    
    HiddenBar;
    
    NSIndexPath * indexPath = [self.tableView indexPathForCell:cell];
    
    CouponModel * model = self.couponArray[indexPath.row];
    
    SettingCouponViewController * addVC = [SettingCouponViewController new];
    
    addVC.titleType = @"edit";
    addVC.model = model;
    
    [self.navigationController pushViewController:addVC animated:YES];
    
}

@end
